/*
 * Mp3Vue1.java Clase Vue, Afficher les composants
 *
 * Created on 15 avril 2008, 03:58
 */

package gestion_de_mp3.Vue;

import gestion_de_mp3.Modeles.Champ;
import gestion_de_mp3.Modeles.Entite;
import gestion_de_mp3.Modeles.LireXml;
import gestion_de_mp3.Modeles.Liste;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author  Duc, Nicola
 */
public class Mp3Vue extends javax.swing.JFrame {
    public LireXml xml;
    /** Creates new form Mp3Vue1 */
    public Mp3Vue(LireXml xml) {
        this.xml=xml;
        initComponents();
    }
    
    /** 
     * Initialisation les composant avec les valeurs par default
     * 
     * 
     */
    // <editor-fold defaultstate="collapsed" desc="InitComponent">
    private void initComponents() {

        btExit = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbEntite = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        lsListe = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        lsContenu = new javax.swing.JList();
        txtName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btLogin = new javax.swing.JButton();
        txtPass = new javax.swing.JPasswordField();
        btModifier = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btExit.setText("Exit");
        btExit.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                exitApp(evt);
            }
        });
        btExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExitActionPerformed(evt);
            }
        });

        tbEntite.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbEntite.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
        tbEntite.setDragEnabled(true);
        tbEntite.setEditingColumn(0);
        tbEntite.setEditingRow(0);
        tbEntite.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                actionLire(evt);
            }
        });
        tbEntite.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
                tbEntiteAncestorRemoved(evt);
            }
        });
        jScrollPane1.setViewportView(tbEntite);

        lsListe.setModel(new javax.swing.AbstractListModel() {
            
            String[] strings = { xml.getList().get(0).getBalise(), xml.getList().get(1).getBalise()};
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lsListe.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                afficherListe(evt);
            }
        });
        jScrollPane2.setViewportView(lsListe);

        lsContenu.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "", "" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lsContenu.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                afficherContenu(evt);
            }
        });
        jScrollPane3.setViewportView(lsContenu);

        jLabel1.setText("Login");

        jLabel2.setText("Pseudo");

        jLabel3.setText("Mot de pass");

        btLogin.setText("Login");
        btLogin.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btLoginMouseClicked(evt);
            }
        });
        btLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLoginActionPerformed(evt);
            }
        });

       

        btModifier.setText("Modifier");
        btModifier.setEnabled(false);
        btModifier.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btModifierMouseClicked(evt);
            }
        });
        btModifier.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            @Override
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                btModifierMouseDragged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(157, 157, 157)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3)
                                .addGap(48, 48, 48))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2))
                                .addGap(30, 30, 30))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtPass, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                                    .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btLogin)
                                .addGap(27, 27, 27))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(266, 266, 266)
                        .addComponent(btExit, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 550, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(456, 456, 456)
                        .addComponent(btModifier, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addGap(8, 8, 8)
                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btLogin))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btModifier, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(btExit, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(63, 63, 63))
        );

        pack();
    }// </editor-fold>


    private void btExitActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
}                                      

    private void exitApp(java.awt.event.MouseEvent evt) {                         
       System.exit(0);
    }                        
/**
 * Afficher les chanson dans le composant List
 * @param evt
 */
    private void afficherListe(java.awt.event.MouseEvent evt) {
        Vector<String> titres = new Vector<String>();
        Vector<String> typeMusique = new Vector<String>();
       if(lsListe.getSelectedValue().equals(xml.getList().firstElement().getBalise())){
            Liste types = xml.getList().get(0);
            for(int i=0;i<types.getEntites().size();i++){
                typeMusique.add(types.getEntites().get(i).getChamps().get(1).getData());
            }
            lsContenu.setListData(typeMusique);
       }
       else if(lsListe.getSelectedValue().equals(xml.getList().lastElement().getBalise()))
       {
            Liste collection = xml.getList().get(1);
            
            for(int i=0;i<collection.getSize();i++)
            {
                    String titre=collection.getEntites().get(i).getChamps().get(2).getData();
                    titres.add(titre);
                 
             }
            lsContenu.setListData(titres);
       }
    }
/**
 * Comparation si utilisateur a bien saisit le mot de pass (Comparation lettre par lettre)
 * @param inputPassword
 * @return
 */
    private static boolean isPasswordCorrect(char[] inputPassword) {
		char[] actualPassword = { 'a', 'd', 'm', 'i', 'n' };
		if (inputPassword.length != actualPassword.length)
			return false; // Return false if lengths are unequal
		for (int i = 0; i < inputPassword.length; i++)
			if (inputPassword[i] != actualPassword[i])
				return false;
		return true;
    }
    /**
     * Chercher les Infos de chanson par titre de chanson
     * Les mettre dans Object de deux dimentions
     * @param titre
     * @param xml
     * @return Object de deux dimentions
     */
    public Object[][] chercherMusiqueParNom(String titre,LireXml xml)
   {
    Liste lesTypes = xml.getList().get(0);
    Liste liste =xml.getList().get(1);
    Object[][] resultats = new Object[100][5];
    int indexRow =0;
    for (int i=0;i<liste.getSize();i++)
    {
        Entite entite = liste.getEntites().get(i);
        for (int j=0;j<entite.getChamps().size();j++){
            Champ champ = entite.getChamps().get(j);
            if((champ.getBalise().equals("titre")&&(champ.getData().equals(titre))))
            {
                             
                resultats[indexRow][0]=entite.getChamps().get(0).toString();
                resultats[indexRow][1]=entite.getChamps().get(1).toString();
                resultats[indexRow][2]=entite.getChamps().get(2).toString();
                resultats[indexRow][3]=entite.getChamps().get(3).toString();
                Entite type =entite.getChamps().get(4).getEntite();
                for (int x=0;x<type.getChamps().size();x++){
                    for (int y=0;y<lesTypes.getSize();y++){
                        int id1 =lesTypes.getEntites().get(y).getChamps().get(0).getId();
                        int id2 =type.getChamps().get(x).getId();
                        if (id1==id2){
                              resultats[indexRow][4]=lesTypes.getEntites().get(y).getChamps().get(1).getData();
                              indexRow++;
                        }
                    }
                    
                }
            }
            
        }
    }
 return resultats;
}
/**
 * Chercher les Infos des chanson par leurs type
 * Les mettre dans Object de deux dimentions
 * @param titre
 * @param xml
 * @return Object de deux dimentions
 */
public Object[][] chercherMusiqueParType(String titre,LireXml xml)
   {
    Liste lesTypes = xml.getList().get(0);
    Liste liste =xml.getList().get(1);
    Object[][] resultats = new Object[100][5];
    int indexRow =0;
    for (int i=0;i<liste.getSize();i++)
    {
        Entite entite = liste.getEntites().get(i);
             //Champ champ = entite.getChamps().get(j);
        Entite type =entite.getChamps().get(4).getEntite();
        for (int x=0;x<type.getChamps().size();x++){
             for (int y=0;y<lesTypes.getSize();y++){
                 int id1 =lesTypes.getEntites().get(y).getChamps().get(0).getId();
                 int id2 =type.getChamps().get(x).getId();
                 if ((id1==id2)&&(lesTypes.getEntites().get(y).getChamps().get(1).getData().equals(titre))){
                    resultats[indexRow][0]=entite.getChamps().get(0).toString();
                    resultats[indexRow][1]=entite.getChamps().get(1).toString();
                    resultats[indexRow][2]=entite.getChamps().get(2).toString();
                    resultats[indexRow][3]=entite.getChamps().get(3).toString();
                    Entite type1 =entite.getChamps().get(4).getEntite();
                    for (int m=0;m<type1.getChamps().size();m++){
                        for (int n=0;n<lesTypes.getSize();n++){
                            int id3 =lesTypes.getEntites().get(n).getChamps().get(0).getId();
                            int id4 =type1.getChamps().get(m).getId();
                            if (id3==id4){
                            resultats[indexRow][4]=lesTypes.getEntites().get(n).getChamps().get(1).getData();
                            indexRow++;
                       }
                   }
                    
                }
              }
           }
        }
    }
 return resultats;
}
/**
 * Action : Quand l'utilisateur clique sur une ligne, va chercher l'info
 * et mets dans composant Jtable
 * @param evt
 */
    private void afficherContenu(java.awt.event.MouseEvent evt) {
        String typeMusic = (String)lsListe.getSelectedValue();
        String chanson = (String)lsContenu.getSelectedValue();
        Object[][] objs = null;
        if(typeMusic.equals("touslestypes"))
        {
            objs =chercherMusiqueParType(chanson,xml);
            tbEntite.setModel(new javax.swing.table.DefaultTableModel(objs,
            new String [] {
                "Id", "Artiste", "Titre","Fichier","Type"}
        ));
            
        }
        else
        {
            objs =chercherMusiqueParNom(chanson,xml) ;
            tbEntite.setModel(new javax.swing.table.DefaultTableModel(objs,
            new String [] {
                "Id", "Artiste", "Titre","Fichier","Type"}
        ));
        }
        
    }
    private void btLoginMouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
    }
    private void actionLire(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
    }
    private void btLoginActionPerformed(java.awt.event.ActionEvent evt) {
       char[] password = txtPass.getPassword();
        if ((txtName.getText().equals("admin"))&&(isPasswordCorrect(password)))
      {
        
          btModifier.setEnabled(true);
      }
      else{
        
        JFrame jframe = new JFrame();
        JOptionPane.showMessageDialog(jframe,"Le pseudo ou le mot de pass n'est pas correct !","échec login",
									JOptionPane.PLAIN_MESSAGE);
        btModifier.setEnabled(false);
      }
    }

    private void tbEntiteAncestorRemoved(javax.swing.event.AncestorEvent evt) {
        // TODO add your handling code here:
    }
    private void btModifierMouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
    }

    private void btModifierMouseDragged(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
    }
    
        
    private javax.swing.JButton btExit;
    private javax.swing.JButton btLogin;
    private javax.swing.JButton btModifier;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList lsContenu;
    private javax.swing.JList lsListe;
    private javax.swing.JTable tbEntite;
    private javax.swing.JTextField txtName;
    private javax.swing.JPasswordField txtPass;
    // End of variables declaration
    
    
}
